package com.clw.weblogin.service.impl;

import com.clw.weblogin.common.entity.ResultEntity;
import com.clw.weblogin.dao.HuanKuanMapper;
import com.clw.weblogin.entity.HuanKuan;
import com.clw.weblogin.entity.HuanKuanExample;
import com.clw.weblogin.service.HuanKuanService;
import com.clw.weblogin.service.JieKuanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class HuanKuanServiceImpl implements HuanKuanService {
    @Autowired
    HuanKuanMapper huanKuanMapper;
    @Autowired
    JieKuanService jieKuanService;

    public List<HuanKuan> selectByExample(HuanKuan huanKuan) {
        HuanKuanExample example = new HuanKuanExample();
        HuanKuanExample.Criteria criteria = example.createCriteria();
        criteria.andTypeEqualTo(huanKuan.getType());
        List<HuanKuan> list=huanKuanMapper.selectByExample(example);
        return list;
    }

    public ResultEntity selectByDate(HuanKuan huanKuan, int page, int limit) {
        HuanKuanExample example = new HuanKuanExample();
        Calendar calendar = Calendar.getInstance();
        HuanKuanExample.Criteria criteria = example.createCriteria();
        criteria.andDepartmentidNotEqualTo("1");
        criteria.andTypeEqualTo(0);
        criteria.andDaikuanenddateLessThanOrEqualTo(huanKuan.getDaikuanenddate());
        if (huanKuan.getName() != null) {
            criteria.andNameLike("%" + huanKuan.getName() + "%");
        }
        if (huanKuan.getNamenumber() != null) {
            criteria.andNamenumberLike("%" + huanKuan.getNamenumber() + "%");
        }
        if (huanKuan.getDaikuanmoney() != null) {
            criteria.andDaikuanmoneyEqualTo(huanKuan.getDaikuanmoney());
        }
        if (huanKuan.getDaikuanbegindate() != null) {
            criteria.andDaikuanbegindateEqualTo(huanKuan.getDaikuanbegindate());
        }
        example.setStart((page - 1) * limit);
        example.setLimit(limit);

        List<HuanKuan> list = huanKuanMapper.selectByExample(example);
        ResultEntity resultEntity = new ResultEntity();
        if (list.size() > 0) {
            for (int i = 0; i < list.size(); ++i) {
                String yearStr;
                int month;
                String monthStr;
                int day;
                String dayStr;
                if ((list.get(i)).getDaikuanbegindate() != null) {
                    calendar.setTime((list.get(i)).getDaikuanbegindate());
                    yearStr = calendar.get(Calendar.YEAR) + "";
                    month = calendar.get(Calendar.MONTH) + 1;
                    monthStr = month < 10 ? "0" + month : month + "";
                    day = calendar.get(Calendar.DATE);
                    dayStr = day < 10 ? "0" + day : day + "";
                    (list.get(i)).setStrdaikuanbegindate(yearStr + "/" + monthStr + "/" + dayStr);
                }

                if ((list.get(i)).getDaikuanenddate() != null) {
                    calendar.setTime((list.get(i)).getDaikuanenddate());
                    yearStr = calendar.get(Calendar.YEAR) + "";
                    month = calendar.get(Calendar.MONTH) + 1;
                    monthStr = month < 10 ? "0" + month : month + "";
                    day = calendar.get(Calendar.DATE);
                    dayStr = day < 10 ? "0" + day : day + "";
                    (list.get(i)).setStrdaikuanenddate(yearStr + "/" + monthStr + "/" + dayStr);
                }

                if ((list.get(i)).getJiebendate() != null) {
                    calendar.setTime((list.get(i)).getJiebendate());
                    yearStr = calendar.get(Calendar.YEAR) + "";
                    month = calendar.get(Calendar.MONTH) + 1;
                    monthStr = month < 10 ? "0" + month : month + "";
                    day = calendar.get(Calendar.DATE);
                    dayStr = day < 10 ? "0" + day : day + "";
                    (list.get(i)).setStrjiebendate(yearStr + "/" + monthStr + "/" + dayStr);
                }
                if ((list.get(i)).getJiexidate() != null) {
                    calendar.setTime((list.get(i)).getJiexidate());
                    yearStr = calendar.get(Calendar.YEAR) + "";
                    month = calendar.get(Calendar.MONTH) + 1;
                    monthStr = month < 10 ? "0" + month : month + "";
                    day = calendar.get(Calendar.DATE);
                    dayStr = day < 10 ? "0" + day : day + "";
                    (list.get(i)).setStrjiexidate(yearStr + "/" + monthStr + "/" + dayStr);
                }


                try {
                    if ((list.get(i)).getJiebendate() != null) {
                        list.get(i).setBenjinday(jieKuanService.days((list.get(i)).getJiebendate(), new Date()));
                    } else {
                        list.get(i).setBenjinday(jieKuanService.days((list.get(i)).getDaikuanbegindate(), new Date()));
                    }
                    if ((list.get(i)).getJiexidate() != null) {
                        list.get(i).setXiday(jieKuanService.days((list.get(i)).getJiexidate(), new Date()));
                    } else {
                        list.get(i).setXiday(jieKuanService.days((list.get(i)).getDaikuanbegindate(), new Date()));
                    }
                } catch (Exception var15) {
                    System.out.println(var15);
                }
                //      (list.get(i)).setHavelixi(havelixi((list.get(i)).getXiday(), Double.valueOf((list.get(i)).getLilv()), (list.get(i)).getYue()));
                if(list.get(i).getLixi()==null){
                    list.get(i).setLixi(0);
                }
                list.get(i).setHavelixi(havelixi((list.get(i)).getXiday(), Double.valueOf((list.get(i)).getLilv()), (list.get(i)).getYue())+list.get(i).getLixi());
            }
            Long count = huanKuanMapper.countByExample(example);
            resultEntity.setState(200);
            resultEntity.setMessage("获取数据");
            resultEntity.setData(list);
            resultEntity.setCount(count.intValue());
        } else {
            resultEntity.setMessage("无数据");
        }
        return resultEntity;
    }

    public List<HuanKuan> selectById(HuanKuan huanKuan) {
        HuanKuanExample example=new HuanKuanExample();
        HuanKuanExample.Criteria criteria=example.createCriteria();
        criteria.andIdEqualTo(huanKuan.getId());
        if(huanKuan.getType()!=null){
            criteria.andTypeEqualTo(huanKuan.getType());
        }
        return huanKuanMapper.selectByExample(example);
    }

    public ResultEntity select(HuanKuan huanKuan, int page, int limit) {
        HuanKuanExample example = new HuanKuanExample();
        Calendar calendar = Calendar.getInstance();
        HuanKuanExample.Criteria criteria = example.createCriteria();
        criteria.andDepartmentidNotEqualTo("1");
        criteria.andTypeNotEqualTo(2);
        if (huanKuan.getName() != null) {
            criteria.andNameLike("%" + huanKuan.getName() + "%");
        }
        if (huanKuan.getNamenumber() != null) {
            criteria.andNamenumberLike("%" + huanKuan.getNamenumber() + "%");
        }
        if (huanKuan.getDaikuanmoney() != null) {
            criteria.andDaikuanmoneyEqualTo(huanKuan.getDaikuanmoney());
        }
        if (huanKuan.getDaikuanbegindate() != null) {
            criteria.andDaikuanbegindateEqualTo(huanKuan.getDaikuanbegindate());
        }
        example.setStart((page - 1) * limit);
        example.setLimit(limit);

        List<HuanKuan> list = huanKuanMapper.selectByExample(example);
        ResultEntity resultEntity = new ResultEntity();
        if (list.size() > 0) {
            for (int i = 0; i < list.size(); ++i) {
                String yearStr;
                int month;
                String monthStr;
                int day;
                String dayStr;
                if ((list.get(i)).getDaikuanbegindate() != null) {
                    calendar.setTime((list.get(i)).getDaikuanbegindate());
                    yearStr = calendar.get(Calendar.YEAR) + "";
                    month = calendar.get(Calendar.MONTH) + 1;
                    monthStr = month < 10 ? "0" + month : month + "";
                    day = calendar.get(Calendar.DATE);
                    dayStr = day < 10 ? "0" + day : day + "";
                    (list.get(i)).setStrdaikuanbegindate(yearStr + "/" + monthStr + "/" + dayStr);
                }

                if ((list.get(i)).getDaikuanenddate() != null) {
                    calendar.setTime((list.get(i)).getDaikuanenddate());
                    yearStr = calendar.get(Calendar.YEAR) + "";
                    month = calendar.get(Calendar.MONTH) + 1;
                    monthStr = month < 10 ? "0" + month : month + "";
                    day = calendar.get(Calendar.DATE);
                    dayStr = day < 10 ? "0" + day : day + "";
                    (list.get(i)).setStrdaikuanenddate(yearStr + "/" + monthStr + "/" + dayStr);
                }

                if ((list.get(i)).getJiebendate() != null) {
                    calendar.setTime((list.get(i)).getJiebendate());
                    yearStr = calendar.get(Calendar.YEAR) + "";
                    month = calendar.get(Calendar.MONTH) + 1;
                    monthStr = month < 10 ? "0" + month : month + "";
                    day = calendar.get(Calendar.DATE);
                    dayStr = day < 10 ? "0" + day : day + "";
                    (list.get(i)).setStrjiebendate(yearStr + "/" + monthStr + "/" + dayStr);
                }
                if ((list.get(i)).getJiexidate() != null) {
                    calendar.setTime((list.get(i)).getJiexidate());
                    yearStr = calendar.get(Calendar.YEAR) + "";
                    month = calendar.get(Calendar.MONTH) + 1;
                    monthStr = month < 10 ? "0" + month : month + "";
                    day = calendar.get(Calendar.DATE);
                    dayStr = day < 10 ? "0" + day : day + "";
                    (list.get(i)).setStrjiexidate(yearStr + "/" + monthStr + "/" + dayStr);
                }


                try {
                    if ((list.get(i)).getJiebendate() != null) {
                        list.get(i).setBenjinday(jieKuanService.days((list.get(i)).getJiebendate(), new Date()));
                    } else {
                        list.get(i).setBenjinday(jieKuanService.days((list.get(i)).getDaikuanbegindate(), new Date()));
                    }
                    if ((list.get(i)).getJiexidate() != null) {
                        list.get(i).setXiday(jieKuanService.days((list.get(i)).getJiexidate(), new Date()));
                    } else {
                        list.get(i).setXiday(jieKuanService.days((list.get(i)).getDaikuanbegindate(), new Date()));
                    }
                } catch (Exception var15) {
                    System.out.println(var15);
                }
          //      (list.get(i)).setHavelixi(havelixi((list.get(i)).getXiday(), Double.valueOf((list.get(i)).getLilv()), (list.get(i)).getYue()));
                if(list.get(i).getLixi()==null){
                    list.get(i).setLixi(0);
                }
                list.get(i).setHavelixi(havelixi((list.get(i)).getXiday(), Double.valueOf((list.get(i)).getLilv()), (list.get(i)).getYue())+list.get(i).getLixi());
            }
            Long count = huanKuanMapper.countByExample(example);
            resultEntity.setState(200);
            resultEntity.setMessage("获取数据");
            resultEntity.setData(list);
            resultEntity.setCount(count.intValue());
        } else {
            resultEntity.setMessage("无数据");
        }
        return resultEntity;
    }

    public ResultEntity insert(HuanKuan huanKuan) {
        ResultEntity resultEntity = new ResultEntity();
        int i = huanKuanMapper.insert(huanKuan);
        if (i == 1) {
            resultEntity.setState(200);
            resultEntity.setMessage("添加成功");
        } else {
            resultEntity.setState(100);
            resultEntity.setMessage("添加失败");
        }
        return resultEntity;
    }

    public ResultEntity update(HuanKuan huanKuan) {
        ResultEntity resultEntity = new ResultEntity();
        int i = huanKuanMapper.updateByPrimaryKey(huanKuan);
        if (i == 1) {
            resultEntity.setState(200);
            resultEntity.setMessage("成功");
        } else {
            resultEntity.setState(100);
            resultEntity.setMessage("失败");
        }
        return resultEntity;
    }

    public Date yujidate(Date cunkuandate, int jiesuanzhouqi) throws Exception {
        Date yujidate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar_cunkuan = Calendar.getInstance();
        calendar_cunkuan.setTime(cunkuandate);
        int day_cunkuan = calendar_cunkuan.get(Calendar.DATE);
        int month_cunkuan = calendar_cunkuan.get(Calendar.MONTH) + 1;//获取月份
        int year_cunkuan = calendar_cunkuan.get(Calendar.YEAR);//获取年份
        int maxDay_cunkuan = calendar_cunkuan.getActualMaximum(Calendar.DAY_OF_MONTH); //当前月的最大天数
        if (jiesuanzhouqi == 30) {
            if (month_cunkuan == 2 && day_cunkuan >= 28) {   //2月份28 存款
                int mon = month_cunkuan + 1;
                int daya = 28;
                yujidate = sdf.parse(year_cunkuan + "-" + mon + "-" + 30);
            } else if (day_cunkuan >= 30) {
                if (month_cunkuan >= 12) {             //10月份以上存款， 要加一年
                    int mon = month_cunkuan + 1 - 12;
                    int year = year_cunkuan + 1;
                    calendar_cunkuan.setTime(sdf.parse(year + "-" + mon + "-" + 1));
                    int maxday = calendar_cunkuan.getActualMaximum(Calendar.DAY_OF_MONTH);
                    if (maxday < 30) {
                        yujidate = sdf.parse(year + "-" + mon + "-" + maxday);
                    } else {
                        yujidate = sdf.parse(year + "-" + mon + "-" + 30);
                    }
                } else {                        //其他月份存款， 月份+3， 天数30
                    int mon = month_cunkuan + 1;
                    yujidate = sdf.parse(year_cunkuan + "-" + mon + "-" + 30);
                }
            } else { //天数<30
                if (month_cunkuan >= 12) {             //10月份以上存款， 要加一年
                    int mon = month_cunkuan + 1 - 12;
                    int year = year_cunkuan + 1;
                    yujidate = sdf.parse(year + "-" + mon + "-" + day_cunkuan);
                } else {                        //其他月份存款， 月份+3， 天数30
                    int mon = month_cunkuan + 1;
                    yujidate = sdf.parse(year_cunkuan + "-" + mon + "-" + day_cunkuan);
                }
            }
        }
        return yujidate;
    }

    public int havelixi(int day, double lilv, double yue) {
        //拿全部利息
        if ( Math.round(yue * lilv / 30 * day) < 0) {
            return 0;
        } else {
            return (int) Math.round(yue * lilv / 30 * day);
        }
    }

    public ResultEntity updatesome(HuanKuan huanKuan) {
        ResultEntity entity=new ResultEntity();
        HuanKuanExample example=new HuanKuanExample();
        HuanKuanExample.Criteria criteria=example.createCriteria();
        criteria.andIdEqualTo(huanKuan.getId());
        List<HuanKuan> list=huanKuanMapper.selectByExample(example);
        HuanKuan updateBean=list.get(0);
        updateBean.setDaikuanenddate(huanKuan.getDaikuanenddate());
        updateBean.setYue(huanKuan.getYue());
        updateBean.setJiebendate(huanKuan.getJiebendate());
        if(huanKuan.getType()!=null){
            updateBean.setType(huanKuan.getType());
        }
        int i=huanKuanMapper.updateByPrimaryKey(updateBean);
        if(i==1){
            entity.setState(200);
            entity.setMessage("还本金成功");
        }
        return entity;
    }

    public ResultEntity huanlixi(HuanKuan huanKuan) {
        ResultEntity entity=new ResultEntity();
        HuanKuanExample example=new HuanKuanExample();
        HuanKuanExample.Criteria criteria=example.createCriteria();
        criteria.andIdEqualTo(huanKuan.getId());
        List<HuanKuan> list=huanKuanMapper.selectByExample(example);
        HuanKuan updateBean=list.get(0);
        updateBean.setJiexidate(huanKuan.getJiexidate());
        updateBean.setLixi(huanKuan.getLixi());
        int i=huanKuanMapper.updateByPrimaryKey(updateBean);
        if(i==1){
            entity.setState(200);
            entity.setMessage("还利息成功");
        }
        return entity;
    }
}
